/**
 * 
 */
package com.neal.web.controller.message;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.neal.domain.Message;
import com.neal.service.IMessageBoardService;

/**
 * @author Neal
 * @version 1.0
 * @description
 */
@Controller
@RequestMapping("/messagePost")
public class MessagePostController {

	private IMessageBoardService messageBoardService;

	@Autowired
	public MessagePostController(IMessageBoardService messageBoardService) {
		this.messageBoardService = messageBoardService;
	}
	
	@RequestMapping(method=RequestMethod.GET)
	public String setupForm(Model model) {
		Message message = new Message();
		model.addAttribute("message", message);
		return "message/messagePost";
	}
	
	@RequestMapping(method=RequestMethod.POST)
	public String onSubmit(@ModelAttribute Message message, Model model,BindingResult result){
		if(result.hasErrors()){
			return "messagePost";
		}else{
			messageBoardService.postMessage(message);
			return "redirect:messageList";
		}
	}
}
